/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  SetupLogFormat.java                                      
 *  File Contents Outline :   设置日志格式         
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */
package cn.edu.thu.log.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
import storage.dbhelper.*;
import storage.dbobject.*;
import storage.util.TimeUtil;


public class SetupLogFormat {

    static String[] logHeadName = {"TimeStamp", "RemoteIP", "OriginateIP", "UA", "SessionID", "TransactionID", "MobileNo", "Source", "ContentType", "Ptn", "Encoding", "Referer",
        "UID", "PGID", "DeviceID", "Latitude", "Longitude"};
    static String[] logPageBodyName = {"AppRespTime", "From", "HasAds", "Hits", "IsAdvance", "PageNum", "Query", "QueryType", "Rank", "RespTime", "Shard", "SnapshotURL", "Start", "Summary", "URL", "UseSource"};
    static String[] logMusicBodyName = {"Type", "From", "Hits", "PageNum", "Query", "OpType", "Id", "Title", "songid", "RespTime", "Start"};
    static String[] logImageBodyName = {"From", "Hits", "PageNum", "Query", "Rank", "RespTime", "Start", "URL", "PageDepth", "DetailType", "DetailSizeCat", "DetailSize", "DetailColor", "DetailPubTime", "ShareVia", "ShareTo", "APIUsed"};
    static String[] logNewsBodyName = {"URL", "From", "Hits", "PageNum", "Query", "OpType", "Title", "RespTime", "Start", "Rank", "Summary", "PageType"};
    static String[] logOtherBodyName = {"SearchUrl", "Channel", "PageType", "LoadTime", "PageDepth", "Blockid", "Query", "Title", "Summary", "DocumentID", "SubContentID", "ClickType", "Block", "OpType", "ShareVia", "Share", "APIUsed", "revcommon", "Publisher"};
    static String[] logRealBodyName = {"Type", "Hits", "PageNum", "Query", "OpType", "RespTime", "Start", "Result", "Relation", "PageDepth", "DetailType", "DetailSource", "ClickType", "BuzzID", "NewsID", "url", "Publisher", "ShareVia", "Share", "APIUsed", "revcommon", "Summary"};
    static Map<Integer, String[]> productLocation = new HashMap<Integer, String[]>();

    public static void setProductLocation() {
        productLocation.put(0, logPageBodyName);
        productLocation.put(1, logImageBodyName);
        productLocation.put(2, logMusicBodyName);  
        productLocation.put(3, logNewsBodyName);
        productLocation.put(4, logRealBodyName);       
        productLocation.put(5, logOtherBodyName);
        
    }

    public static void setUpLogHead() throws UnsupportedEncodingException {

        int[] logHeadLocation = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
        String[] logHeadChinese = {"时间戳", "远端IP地址", "原始IP", "User-Agent信息", "会话ID", "搜索流水号", "手机号码", "来源", "内容类型", "来路网址约定域名", "编码", "来路网址（空为站内)", "登陆用户ID	", "Cookie中盘古用户临时ID", "用户设备ID", "纬度", "经度"};
        System.out.println("logHeadName.length:" + logHeadName.length);
        System.out.println("logHeadLocation.length:" + logHeadLocation.length);
        System.out.println("logHeadChinese.length:" + logHeadChinese.length);
        //logHead
        LogHeadHelper logHeadH = new LogHeadHelper();
        for (int i = 0; i < logHeadLocation.length; i++) {
            String name = logHeadName[i];
            String location = String.valueOf(logHeadLocation[i]);
            String chineseTemp = logHeadChinese[i];
            String chinese = URLEncoder.encode(chineseTemp, "UTF-8");

            List<DbLogHead> LogHeads = logHeadH.getLogHeads(DbLogHead.cl_name, name);
            if (LogHeads.size() > 0) {
                System.out.println("logHead already exit");
                DbLogHead uniqueLogHead = LogHeads.get(0);
                logHeadH.saveLogHead(new DbLogHead(uniqueLogHead.getRowKey(), name, location, chinese));
                for (int j = 1; j < LogHeads.size(); j++) {
                    logHeadH.deleteLogHead(LogHeads.get(j).getRowKey());
                }
            } else {
                System.out.println("add new logHead");
                logHeadH.saveLogHead(new DbLogHead(name, location, chinese));
            }
        }
    }
    //version

    public static void setUpVersion() {
        String versionName = "version1.5";
        VersionHelper versionh = new VersionHelper();
        ArrayList<String> logHeadList = new ArrayList<String>();
        for (int i = 0; i < logHeadName.length; i++) {
            logHeadList.add(logHeadName[i]);
        }

        List<DbVersion> versions = versionh.getVersions(DbVersion.cl_versionNum, versionName);
        if (versions.size() > 0) {
            System.out.println("version already exit");
            DbVersion version = versions.get(0);
            versionh.saveVersion(new DbVersion(version.getRowKey(), versionName, String.valueOf(""), String.valueOf(""), TimeUtil.getTime(), logHeadList));
            for (int j = 1; j < versions.size(); j++) {
                versionh.deleteVersion(versions.get(j).getRowKey());
            }
        } else {
            System.out.println("add new version");
            versionh.saveVersion(new DbVersion(versionName, String.valueOf(""), String.valueOf(""), TimeUtil.getTime(), logHeadList));
        }
    }

    //logBody 
    public static void setUpPageLogBody() throws UnsupportedEncodingException {
        String[] logBodyAbbreviations = {"art", "fr", "hc", "hit", "adv", "pn", "q", "qt", "rk", "rt", "sha", "su", "sa", "sm", "url", "us"};
        String[] logBodyChinese = {"前端响应时间(ms)", "酌情补充", "是否带广告", "结果记录数", "是否高级搜索", "每页记录数", "查询关键字", "master返回的查询词类型", "点击URL在返回网页结果的排名", "响应时间", "点击hit的shard信息", "快照URL", "开始位置", "结果网页的总结", "点击的url", "搜索串来源"};
        System.out.println("logPageBodyName.length:" + logPageBodyName.length);
        System.out.println("logBodyAbbreviations.length:" + logBodyAbbreviations.length);
        System.out.println("logBodyChinese.length:" + logBodyChinese.length);

        LogBodyHelper logBodyH = new LogBodyHelper();
        for (int i = 0; i < logPageBodyName.length; i++) {
            String name = logPageBodyName[i];
            String abbre = logBodyAbbreviations[i];
            String chineseTemp = logBodyChinese[i];
            String chinese = URLEncoder.encode(chineseTemp, "UTF-8");
            String hasValue = "no";
            Hashtable<String, String> valueDescrptionMap = new Hashtable<String, String>();
            List<DbLogBody> logBodys = logBodyH.getLogBodys(DbLogBody.cl_name, name);
            if (logBodys.size() > 0) {
                System.out.println("logBody already exit");
                DbLogBody logBody = logBodys.get(0);
                logBodyH.saveLogBody(new DbLogBody(logBody.getRowKey(), name, abbre, chinese, hasValue, valueDescrptionMap));
                for (int j = 1; j < logBodys.size(); j++) {
                    logBodyH.deleteLogBody(logBodys.get(j).getRowKey());
                }
            } else {
                System.out.println("add new logBody");
                logBodyH.saveLogBody(new DbLogBody(name, abbre, chinese, hasValue, valueDescrptionMap));
            }
        }
    }
    //logBody 

    public static void setUpImageLogBody() throws UnsupportedEncodingException {
        String[] logBodyAbbreviations = {"fr", "hit", "pn", "q", "rk", "rt", "sa", "url", "pd", "dt", "dsc", "ds", "dc", "dp", "sv", "se", "api"};
        String[] logBodyChinese = {"搜索类型", "搜索结果总记录数", "每页记录数", "查询关键词", "排名", "响应时间", "记录开始位置", "点击的URL", "页面深度", "图片类别名称", "图片大小描述", "图片大小尺寸", "图片颜色", "图片发布时间", "分享媒介", "分享目标地址", "应用的API"};
        System.out.println("logImageBodyName.length:" + logImageBodyName.length);
        System.out.println("logBodyAbbreviations.length:" + logBodyAbbreviations.length);
        System.out.println("logBodyChinese.length:" + logBodyChinese.length);

        LogBodyHelper logBodyH = new LogBodyHelper();
        for (int i = 0; i < logImageBodyName.length; i++) {
            String name = logImageBodyName[i];
            String abbre = logBodyAbbreviations[i];
            String chineseTemp = logBodyChinese[i];
            String chinese = URLEncoder.encode(chineseTemp, "UTF-8");
            String hasValue = "no";
            Hashtable<String, String> valueDescrptionMap = new Hashtable<String, String>();
            List<DbLogBody> logBodys = logBodyH.getLogBodys(DbLogBody.cl_name, name);
            if (logBodys.size() > 0) {
                System.out.println("logBody already exit");
                DbLogBody logBody = logBodys.get(0);
                logBodyH.saveLogBody(new DbLogBody(logBody.getRowKey(), name, abbre, chinese, hasValue, valueDescrptionMap));
                for (int j = 1; j < logBodys.size(); j++) {
                    logBodyH.deleteLogBody(logBodys.get(j).getRowKey());
                }
            } else {
                System.out.println("add new logBody");
                logBodyH.saveLogBody(new DbLogBody(name, abbre, chinese, hasValue, valueDescrptionMap));
            }
        }
    }
    //logBody 

    public static void setUpNewsLogBody() throws UnsupportedEncodingException {
        String[] logBodyAbbreviations = {"url", "fr", "hit", "pn", "q", "op", "tt", "rt", "sa", "r", "sm", "pt"};
        String[] logBodyChinese = {"点击的URL", "搜索类型", "搜索结果总记录数", "每页记录数", "查询关键词", "操作类型", "标题", "响应时间", "记录开始位置", "点击URL在返回网页结果中的排名", "点击结果网页的概括", "用户订阅栏目ID"};
        System.out.println("logNewsBodyName.length:" + logNewsBodyName.length);
        System.out.println("logBodyAbbreviations.length:" + logBodyAbbreviations.length);
        System.out.println("logBodyChinese.length:" + logBodyChinese.length);

        LogBodyHelper logBodyH = new LogBodyHelper();
        for (int i = 0; i < logNewsBodyName.length; i++) {
            String name = logNewsBodyName[i];
            String abbre = logBodyAbbreviations[i];
            String chineseTemp = logBodyChinese[i];
            String chinese = URLEncoder.encode(chineseTemp, "UTF-8");
            String hasValue = "no";
            Hashtable<String, String> valueDescrptionMap = new Hashtable<String, String>();
            List<DbLogBody> logBodys = logBodyH.getLogBodys(DbLogBody.cl_name, name);
            if (logBodys.size() > 0) {
                System.out.println("logBody already exit");
                DbLogBody logBody = logBodys.get(0);
                logBodyH.saveLogBody(new DbLogBody(logBody.getRowKey(), name, abbre, chinese, hasValue, valueDescrptionMap));
                for (int j = 1; j < logBodys.size(); j++) {
                    logBodyH.deleteLogBody(logBodys.get(j).getRowKey());
                }
            } else {
                System.out.println("add new logBody");
                logBodyH.saveLogBody(new DbLogBody(name, abbre, chinese, hasValue, valueDescrptionMap));
            }
        }
    }
    //logBody 

    public static void setUpMusicLogBody() throws UnsupportedEncodingException {
        String[] logBodyAbbreviations = {"t", "fr", "hit", "pn", "q", "op", "id", "tt", "sid", "rt", "sa"};
        String[] logBodyChinese = {"搜索类型（数字）", "搜索类型", "搜索结果总记录数", "每页记录数", "查询关键词", "操作类型", "ID", "标题", "歌曲ID", "响应时间", "记录开始位置"};
        System.out.println("logMusicBodyName.length:" + logMusicBodyName.length);
        System.out.println("logBodyAbbreviations.length:" + logBodyAbbreviations.length);
        System.out.println("logBodyChinese.length:" + logBodyChinese.length);

        LogBodyHelper logBodyH = new LogBodyHelper();
        for (int i = 0; i < logMusicBodyName.length; i++) {
            String name = logMusicBodyName[i];
            String abbre = logBodyAbbreviations[i];
            String chineseTemp = logBodyChinese[i];
            String chinese = URLEncoder.encode(chineseTemp, "UTF-8");
            String hasValue = "no";
            Hashtable<String, String> valueDescrptionMap = new Hashtable<String, String>();
            List<DbLogBody> logBodys = logBodyH.getLogBodys(DbLogBody.cl_name, name);
            if (logBodys.size() > 0) {
                System.out.println("logBody already exit");
                DbLogBody logBody = logBodys.get(0);
                logBodyH.saveLogBody(new DbLogBody(logBody.getRowKey(), name, abbre, chinese, hasValue, valueDescrptionMap));
                for (int j = 1; j < logBodys.size(); j++) {
                    logBodyH.deleteLogBody(logBodys.get(j).getRowKey());
                }
            } else {
                System.out.println("add new logBody");
                logBodyH.saveLogBody(new DbLogBody(name, abbre, chinese, hasValue, valueDescrptionMap));
            }
        }
    }
    //logBody 

    public static void setUpRealLogBody() throws UnsupportedEncodingException {
        String[] logBodyAbbreviations = {"t", "hit", "pn", "q", "op", "rt", "sa", "rs", "rn", "pd", "dt", "dso", "ct", "bid", "nid", "url", "pr", "sv", "se", "api", "rc", "sm"};
        String[] logBodyChinese = {"搜索类型", "搜索结果总记录数", "每页记录数", "查询（订阅）关键词", "操作类型", "响应时间", "开始位置", "当前结果页面的Buzz列表", "当前结果页面Buzz和新闻关联列表", "当前搜索的页面深度", "时评搜索类别名称",
            "SNS厂商名称", "点击类型", "操作相关Buzz标识", "用户点击的新闻URL标识信息", "被操作的微博、微群、微博账户地址", "原始微博发布者", "原始微博的概括", "媒介类型（SMS/SNS/MMS/CloudCool", "媒介具体名称", "应用的API", "API调用时发送的消息体内容"};
        System.out.println("logRealBodyName.length:" + logRealBodyName.length);
        System.out.println("logBodyAbbreviations.length:" + logBodyAbbreviations.length);
        System.out.println("logBodyChinese.length:" + logBodyChinese.length);

        LogBodyHelper logBodyH = new LogBodyHelper();
        for (int i = 0; i < logRealBodyName.length; i++) {
            String name = logRealBodyName[i];
            String abbre = logBodyAbbreviations[i];
            String chineseTemp = logBodyChinese[i];
            String chinese = URLEncoder.encode(chineseTemp, "UTF-8");
            String hasValue = "no";
            Hashtable<String, String> valueDescrptionMap = new Hashtable<String, String>();
            List<DbLogBody> logBodys = logBodyH.getLogBodys(DbLogBody.cl_name, name);
            if (logBodys.size() > 0) {
                System.out.println("logBody already exit");
                DbLogBody logBody = logBodys.get(0);
                logBodyH.saveLogBody(new DbLogBody(logBody.getRowKey(), name, abbre, chinese, hasValue, valueDescrptionMap));
                for (int j = 1; j < logBodys.size(); j++) {
                    logBodyH.deleteLogBody(logBodys.get(j).getRowKey());
                }
            } else {
                System.out.println("add new logBody");
                logBodyH.saveLogBody(new DbLogBody(name, abbre, chinese, hasValue, valueDescrptionMap));
            }
        }
    }
    //logBody 

    public static void setUpOtherLogBody() throws UnsupportedEncodingException {
        String[] logBodyAbbreviations = {"u", "cl", "pt", "lt", "pd", "bk", "q", "tt", "sm", "did", "scd", "ct", "bk", "op", "sv", "se", "api", "rc", "pr"};
        String[] logBodyChinese = {"页面所在的路径", "当前点击所在页面", "被点击页面ID", "用户页面载入时间（MS）", "点击“显示更多”次数", "页面模块ID", "搜索词", "标题", "原始微博的总结", "稿件ID", "具体点击条目ID",
            "点击对象类型", "页面模块ID", "用户操作类型", "媒介类型（SMS/SNS/MMS/CloudCool", "媒介具体名称", "应用的API", "API调用时发送的消息体内容", "原始微博的总结"};
        System.out.println("logOtherBodyName.length:" + logOtherBodyName.length);
        System.out.println("logBodyAbbreviations.length:" + logBodyAbbreviations.length);
        System.out.println("logBodyChinese.length:" + logBodyChinese.length);

        LogBodyHelper logBodyH = new LogBodyHelper();
        for (int i = 0; i < logOtherBodyName.length; i++) {
            String name = logOtherBodyName[i];
            String abbre = logBodyAbbreviations[i];
            String chineseTemp = logBodyChinese[i];
            String chinese = URLEncoder.encode(chineseTemp, "UTF-8");
            String hasValue = "no";
            Hashtable<String, String> valueDescrptionMap = new Hashtable<String, String>();
            List<DbLogBody> logBodys = logBodyH.getLogBodys(DbLogBody.cl_name, name);
            if (logBodys.size() > 0) {
                System.out.println("logBody already exit");
                DbLogBody logBody = logBodys.get(0);
                logBodyH.saveLogBody(new DbLogBody(logBody.getRowKey(), name, abbre, chinese, hasValue, valueDescrptionMap));
                for (int j = 1; j < logBodys.size(); j++) {
                    logBodyH.deleteLogBody(logBodys.get(j).getRowKey());
                }
            } else {
                System.out.println("add new logBody");
                logBodyH.saveLogBody(new DbLogBody(name, abbre, chinese, hasValue, valueDescrptionMap));
            }
        }
    }

    //product
    public static void setUpProduct() throws UnsupportedEncodingException {
        String[] productName = {"page", "image", "music", "news", "real", "other"};
        String[] productChinese = {"网页", "图片", "音乐", "新闻", "时评", "其它页面"};
        ProductHelper producth = new ProductHelper();
        for (int i = 0; i < productName.length; i++) {
            ArrayList<String> logBodyList = new ArrayList<String>();
            System.out.println("productLocation.get:" + productLocation.get(i));
            for (int j = 0; j < productLocation.get(i).length; j++) {
                logBodyList.add(productLocation.get(i)[j]);
            }
            List<DbProduct> pageProducts = producth.getProducts(DbProduct.cl_name, productName[i]);
            if (pageProducts.size() > 0) {
                for (int j = 0; j < pageProducts.size(); j++) {
                    producth.deleteProduct(pageProducts.get(j).getRowKey());
                }
                System.out.println("product already exit");
                DbProduct pageProduct = pageProducts.get(0);
                producth.saveProduct(new DbProduct(pageProduct.getRowKey(), productName[i], productChinese[i], TimeUtil.getTime(), logBodyList));
            } else {
                System.out.println("add product");
                producth.saveProduct(new DbProduct(productName[i], productChinese[i], TimeUtil.getTime(), logBodyList));

            }
        }
//        for (int i = 2; i < productName.length; i++) {
//            String name = productName[i];
//            String chineseTemp = productChinese[i];
//            String chinese = URLEncoder.encode(chineseTemp, "UTF-8");
//
//            List<DbProduct> products = producth.getProducts(DbProduct.cl_name, name);
//            if (products.size() > 0) {
//                for (int j = 0; j < products.size(); j++) {
//                    producth.deleteProduct(products.get(j).getRowKey());
//                }
//                System.out.println("product already exit:" + name);
//                DbProduct product = products.get(0);
//                producth.saveProduct(new DbProduct(product.getRowKey(), name, chinese, TimeUtil.getTime(), new ArrayList<String>()));
//
//            } else {
//                System.out.println("add new product:" + name);
//                producth.saveProduct(new DbProduct(name, chinese, TimeUtil.getTime(), new ArrayList<String>()));
//            }
//        }
    }

    private static void setUpPage_HasAdsPattern() {
        String name = "page_HasAds";
        String product = "page";
        String mainCaseID = "";
        String timeStamp = "TimeStamp";
        String classifytag="";
        ArrayList<String> caseIDList = new ArrayList<String>();
        caseIDList.add("UA");
        caseIDList.add("OriginateIP");
        ArrayList<String> activityList = new ArrayList<String>();
        activityList.add("HasAds");
         ArrayList<String> attributeList = new ArrayList<String>();

        BusinessPatternHelper businessPatternH = new BusinessPatternHelper();
        List<DbBusinessPattern> businessPatterns = businessPatternH.getBusinessPatterns(DbBusinessPattern.cl_name, name);
        if (businessPatterns.size() > 0) {
            for (int j = 0; j < businessPatterns.size(); j++) {
                businessPatternH.deleteBusinessPattern(businessPatterns.get(j).getRowKey());
            }
            System.out.println("pattern already exit:" + name);
            DbBusinessPattern businessPattern = businessPatterns.get(0);
            businessPatternH.saveBusinessPattern(new DbBusinessPattern(businessPattern.getRowKey(), name, product, mainCaseID, timeStamp, TimeUtil.getTime(), caseIDList, activityList,attributeList,classifytag));
        } else {
            System.out.println("add pattern");
            businessPatternH.saveBusinessPattern(new DbBusinessPattern(name, product, mainCaseID, timeStamp, TimeUtil.getTime(), caseIDList, activityList,attributeList,classifytag));

        }
    }
   private static void setUpPContentTypePattern() {
        String name = "contentType";
        String product = "page";
        String mainCaseID = "";
        String timeStamp = "TimeStamp";
        String classifytag="";
        ArrayList<String> caseIDList = new ArrayList<String>();
        caseIDList.add("UA");
        caseIDList.add("OriginateIP");
        ArrayList<String> activityList = new ArrayList<String>();
               ArrayList<String> attributeList = new ArrayList<String>();
        activityList.add("ContentType");

        BusinessPatternHelper businessPatternH = new BusinessPatternHelper();
        List<DbBusinessPattern> businessPatterns = businessPatternH.getBusinessPatterns(DbBusinessPattern.cl_name, name);
        if (businessPatterns.size() > 0) {
            for (int j = 0; j < businessPatterns.size(); j++) {
                businessPatternH.deleteBusinessPattern(businessPatterns.get(j).getRowKey());
            }
            System.out.println("pattern already exit:" + name);
            DbBusinessPattern businessPattern = businessPatterns.get(0);
            businessPatternH.saveBusinessPattern(new DbBusinessPattern(businessPattern.getRowKey(), name, product, mainCaseID, timeStamp, TimeUtil.getTime(), caseIDList, activityList,attributeList, classifytag));
        } else {
            System.out.println("add pattern");
            businessPatternH.saveBusinessPattern(new DbBusinessPattern(name, product, mainCaseID, timeStamp, TimeUtil.getTime(), caseIDList, activityList,attributeList,classifytag));

        }
    }
    public static void main(String[] args) throws IOException {
        setProductLocation();

        setUpLogHead();
        setUpVersion();

        setUpPageLogBody();
        setUpMusicLogBody();
        setUpImageLogBody();
        setUpRealLogBody();
        setUpNewsLogBody();
        setUpOtherLogBody();

        setUpProduct();
        setUpPage_HasAdsPattern();
        setUpPContentTypePattern();
    }
}
